/**
 *
 * Copyright (c) 2009-2012 Envision Mobile Ltd. All rights reserved.
 *
 * Other software and company names mentioned herein or used for developing
 * and/or running the Envision Mobile Ltd's software may be trademarks or trade
 * names of their respective owners.
 *
 * Everything in the source code herein is owned by Envision Mobile Ltd.
 * The recipient of this source code hereby acknowledges and agrees that such
 * information is proprietary to Envision Mobile Ltd. and shall not be used, 
 * disclosed, duplicated, and/or reversed engineered except in accordance
 * with the express written authorization of Envision Mobile Ltd.
 *
 * Module: DrugExtraDTO.java
 * Project: ReyOfLight
 *
 * Description:
 *
 *
 * Developer:   10107896
 * Date:        2012-08-16
 * Version:		1.0
 *
 *
 */
package DTO;

import java.io.ByteArrayInputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Locale;

import org.Consts;
import org.Utility;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.util.Log;


/**
 * @author 10107896
 *
 */
public class ObjectDTO extends BaseDTO implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -8326353474170336768L;
	public int intUuid;
	public Bitmap image;

	
	public ObjectDTO(int id, SQLiteDatabase db) { 
		intUuid = id;
		
		Cursor c = null;
		if ("".equalsIgnoreCase(init_statement)) { 

				c = db.query(Consts.TABLE_OBJECT, new String[]{Consts.COLUMN_IMAGE},
						"_id=?", new String[]{intUuid + ""}, null, null, null);

		}
		
		if (c != null) { 
			if (c.moveToFirst()) { 

				byte[] pic=(c.getBlob(0));   
				  ByteArrayInputStream imageStream = new ByteArrayInputStream(pic);
				  image= BitmapFactory.decodeStream(imageStream);
				  //img=Utility.byteArrayToDrawable(c.getBlob(0));

				
			} else { 
				Log.d("", "== something happened, could not extract from database");
			}
			if(c != null) {
				c.close();
			}
		} else { 
			Log.d("", "== c was null");
		}
	}
	
	public ObjectDTO() {
		intUuid=-1;
		image=null;
	}

	public boolean insertIntoDatabase(SQLiteDatabase db) {
		this.db=db;
		try {
			ContentValues values = new ContentValues();
			byte[] img=Utility.bitmaptoByteArray(image);
			values.put(Consts.COLUMN_IMAGE, img);

			int id = (int) db.insert(Consts.TABLE_OBJECT, null, values);

			if (id>0){
				intUuid=id;
				Log.d("", "== was successfully inserted in db, Object");
			}
			else
				Log.d("", "== unsuccessful in inserting in db, Object");

			return id>0;
		} catch (Exception e) {
			e.printStackTrace();

			Log.d("", "== unsucessful in inserting in db, Object, exception");
		}
		return false;
	}
	
	public static ArrayList<ObjectDTO> getObjectsInDatabase(SQLiteDatabase db) { 
		
		ArrayList<ObjectDTO> objects = new ArrayList<ObjectDTO>();
		
		try { 
			//SELECT id FROM drug_extra WHERE drug_id=?
			//sqlite3_bind_int(statement, 1, drugUUID)
			String sql = "SELECT _id FROM " + Consts.TABLE_OBJECT;
			Cursor c = db.rawQuery(sql, null);
			
			while (c.moveToNext()) { 
				
				int primaryKey = c.getInt(0);
				ObjectDTO obj = new ObjectDTO(primaryKey, db);
				objects.add(obj);
			}
			
			if(c != null) {
				c.close();
			}
			
		} catch (Exception e) { 
			e.printStackTrace();
			Log.d("", "== something went wrong, drugExtra, getDrugExtraDB");
		}

		return objects;
	}

	public void delete(SQLiteDatabase db) {
		//ArrayList<DrawingPlaneDTO> drawingPlanes=DrawingPlaneDTO.getDrawingPlaneForObjectInDatabase(intUuid, db);
		
		DrawingPlaneDTO.deleteDrawingPlanesForObjectInDatabase(intUuid, db);
		deleteObject(intUuid, db);
		
	}

	private void deleteObject(int id, SQLiteDatabase db) {
		try {
			boolean state = db.delete(Consts.TABLE_OBJECT, "_id=?",
					new String[] { id + "" }) > 0;

			if (state)
				Log.d("", "== successfully deleted, dose");
			else
				Log.d("", "== problem deleting, dose");
		} catch (SQLException e) {
			Log.e("Database Error: ", e.toString());
			e.printStackTrace();
		}
		
	}
	
}
